import { createRouter, createWebHashHistory } from 'vue-router'
import {getToken} from "@/utils/auth.js";

// 公共路由
export const constantRoutes = [
  { path: "/", redirect: "/home" },
  { name: 'login', path: "/login", component: () => import("@/views/login/index.vue"), meta: { hideTabBar: true, otherBackGroundColor: true } },
  { name: 'home', path: "/home", component: () => import("@/views/home/index.vue"), meta: { hideTabBar: false, otherBackGroundColor: false } },
  { name: 'tags', path: "/labels", component: () => import("@/views/labels/index.vue"), meta: { hideTabBar: false } },
  { name: 'marks',path: "/marks", component: () => import("@/views/marks/index.vue"),meta: { hideTabBar: false } },
  { name: 'settings', path: "/settings", component: () => import("@/views/settings/index.vue"), meta: { hideTabBar: false },
    children: [
      { name: 'account', path: "account", component: () => import("@/views/settings/myAccount/index.vue"), meta: { hideTabBar: true } },
      { name: 'statistics', path: "statistics", component: () => import("@/views/settings/statistics/index.vue"), meta: { hideTabBar: true } }
    ]
  },
  { name: 'detail', path: "/detail", component: () => import("@/views/detail/index.vue"), meta: { hideTabBar: false },
    children: [
      { name: 'detailById', path: ":id", component: () => import("@/views/detail/index.vue"), meta: { hideTabBar: true } },
    ]
  },
  { name: 'article', path: "/article", component: () => import("@/views/article/index.vue"), meta: { hideTabBar: false },
    children: [
      { name: 'articleById', path: ":id", component: () => import("@/views/article/index.vue"), meta: { hideTabBar: true } }
    ]
  },
  { name: 'account', path: "/account", component: () => import("@/views/settings/myAccount/index.vue"), meta: { hideTabBar: true } },
  { name: 'statistics', path: "/statistics", component: () => import("@/views/settings/statistics/index.vue"), meta: { hideTabBar: true } },
  { name: 'share', path: "/share", component: () => import("@/views/settings/sharingManage/index.vue"), meta: { hideTabBar: true } },
  { name: 'history', path: "/history", component: () => import("@/views/settings/history/index.vue"), meta: { hideTabBar: true } },
  { name: 'parse', path: "/parse", component: () => import("@/views/settings/parseList/index.vue"), meta: { hideTabBar: true } },
  { name: 'preference', path: "/preference", component: () => import("@/views/settings/personalityPreference/index.vue"), meta: { hideTabBar: true } },
  { name: 'lock', path: "/lock", component: () => import("@/views/settings/lock/index.vue"), meta: { hideTabBar: true } },
  { name: 'advanceSetting', path: "/advanceSetting", component: () => import("@/views/settings/advancedSetting/index.vue"), meta: { hideTabBar: true } },
  { name: 'about', path: "/about", component: () => import("@/views/settings/about/index.vue"), meta: { hideTabBar: true } },
  { name: 'theme', path: "/theme", component: () => import("@/views/settings/theme/index.vue"), meta: { hideTabBar: true } },
  { name: 'useGuide', path: "/useGuide", component: () => import("@/views/settings/useGuide/index.vue"), meta: { hideTabBar: true },},
  { name: 'useGuideCard', path: "/card/:id", component: () => import("@/views/settings/useGuide/userGuideCard/index.vue"), meta: { hideTabBar: true } },
]

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = []

const router = createRouter({
  history: createWebHashHistory(),
  // 映射关系: path -> component
  routes: constantRoutes
})
// 全局守卫：登录拦截 本地没有存token,请重新登录
router.beforeEach((to, from, next) => {
	// 判断有没有登录
	if (!getToken()) {
		if (to.name == "login") {
			next();
		} else {
			router.push('login')
		}
	} else {
		next();
	}
});

export default router
